package com.itheima.controller;

import cn.itheima.health.entity.QueryPageBean;
import cn.itheima.health.entity.Result;
import cn.itheima.health.service.MenuAdminService;
import com.itheima.pojo.Menu;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/menuAdmin")
public class MenuAdminControllr {

    @Reference
    private MenuAdminService menuAdminService;

    @RequestMapping("/findPage.do")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            Map map = menuAdminService.findPage(queryPageBean);
            return new Result(true, "菜单信息查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"菜单信息查询失败");
        }
    }


    @RequestMapping("/add.do")
    public Result add(@RequestBody Menu menu, @RequestParam("roleIds")Integer[] roleIds){
        try {
            menuAdminService.add(menu,roleIds);
            return new Result(true,"新增菜单项成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"新增菜单项失败");
        }
    }

    @RequestMapping("/findRoleIdsByMenuId.do")
    public Result findRoleIdsByMenuId(@RequestParam("id")Integer id){
        try {
            Map map = menuAdminService.findRoleIdsByMenuId(id);
            return new Result(true,"查询角色成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询角色失败");
        }
    }

    @RequestMapping("/edit.do")
    public Result edit(@RequestBody Menu menu,@RequestParam("roleIds") Integer[] roleIds){
        try {
            menuAdminService.edit(menu,roleIds);
            return new Result(true,"编辑菜单项成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"编辑菜单项失败");
        }
    }

    @RequestMapping("/deleteMenuAdminById.do")
    public Result deleteMenuAdminById(@RequestParam("id") Integer id){
        try {
            menuAdminService.deleteMenuAdminById(id);
            return new Result(true,"删除菜单项成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除用户失败");
        }
    }
}
